package IBBB.view;

import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.HTMLEditor;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

public class NotePane {
    @FXML
    private BorderPane root;
    @FXML
    private ToolBar saveButton;
    @FXML
    private Button saveAsButton;
    @FXML
    private Button openButton;
    @FXML
    private HTMLEditor editor;
    boolean isOpen = false;
    private String openPath = null;

    @FXML
    private void initialize() {
    }

    @FXML
    private void setSaveButtonAction() {
        if (isOpen) {
            File f = new File(openPath);
            try (FileWriter fileWriter = new FileWriter(f)) {
                String t = editor.getHtmlText();
                fileWriter.write(t);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            DirectoryChooser s = new DirectoryChooser();
            File toSave = s.showDialog(root.getScene().getWindow());
            if (toSave != null) {
                String path = toSave.getAbsolutePath();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
                String date = format.format(Calendar.getInstance().getTime());
                path =  path + "/笔记" + date + ".html";
                File f = new File(path);
                try(FileWriter writer = new FileWriter(f))  {
                    f.createNewFile();
                    String text = editor.getHtmlText();
                    System.out.println(text);
                    writer.write(text);
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @FXML
    private void setSaveAsButtonAction() {
        DirectoryChooser s = new DirectoryChooser();
        File toSave = s.showDialog(root.getScene().getWindow());
        if (toSave != null) {
            String path = toSave.getAbsolutePath();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
            String date = format.format(Calendar.getInstance().getTime());
            path = path + "/笔记" + date + ".html";
            File f = new File(path);
            try (FileWriter writer = new FileWriter(f)) {
                f.createNewFile();
                String text = editor.getHtmlText();
                System.out.println(text);
                writer.write(text);
                writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @FXML
    private void setOpenButton() {
        FileChooser chooser = new FileChooser();
        chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("HTML", "*.html"));
        File open = chooser.showOpenDialog(root.getScene().getWindow());
        openPath = open.getAbsolutePath();
        isOpen = true;
        try {
            String text = Files.readString(open.toPath());
            editor.setHtmlText(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
